/* manage 角色处理任务 */
export default class CreepManage extends Creep {
    /* 处理链接送仓任务 */
    public handleLToS():void{
        if (!this.memory.Task) this.memory.Task = {}
        var center_link = Game.getObjectById(this.room.memory.TaskMemory.center_link as string) as StructureLink
        var storage_ = Game.getObjectById(this.room.memory.TaskMemory.storageID as string) as StructureStorage
        if (!center_link || !storage_ || center_link.store.getUsedCapacity(RESOURCE_ENERGY) < 200)
        {
            Game.rooms[this.memory.belong].deleteMisson('CREEP','链接送仓')
            return
        }
        this.say("链接->仓")
        this.wipeResource('energy',storage_)
        this.workstate(RESOURCE_ENERGY)
        if (this.memory.working)
        {
            this.transfer__(storage_,RESOURCE_ENERGY)
        }
        else
        {
            this.withdraw__(center_link,RESOURCE_ENERGY)
        }
        this.LastMisson(2)
    }

    /* 处理近塔填充任务 */
    public handleSToNT():void{
        var NTower = Game.getObjectById(this.room.memory.TaskMemory.NtowerID as string) as StructureTower
        var storage_ = Game.getObjectById(this.room.memory.TaskMemory.storageID as string) as StructureStorage
        if (!NTower || !storage_ || NTower.store.getFreeCapacity(RESOURCE_ENERGY) < 100)
        {
            Game.rooms[this.memory.belong].deleteMisson('CREEP','近塔填充')
            return
        }
        this.say("仓->塔")
        this.wipeResource('energy',storage_)
        this.workstate(RESOURCE_ENERGY)
        if (this.memory.working)
        {
            this.transfer__(NTower,RESOURCE_ENERGY)
        }
        else
        {
            this.withdraw__(storage_,RESOURCE_ENERGY)
        }
        this.LastMisson(8)
    }

    /* 处理终端送仓任务 */
    public handleTToS():void{
        var terminal_ = Game.getObjectById(this.room.memory.TaskMemory.terminalID as string) as StructureTerminal
        var storage_ = Game.getObjectById(this.room.memory.TaskMemory.storageID as string) as StructureStorage
        var myTask = this.room.getMisson('CREEP','终端送仓')
        if (!terminal_ || !storage_ || terminal_.store.getUsedCapacity(myTask.relateResource[0]) < Game.rooms[this.memory.belong].memory.TaskData.terminalResource[myTask.relateResource[0]].num)
        {
            Game.rooms[this.memory.belong].deleteMisson('CREEP','终端送仓')
            return
        }
        this.say('终端->仓')
        this.wipeResource(myTask.relateResource[0],storage_)
        this.workstate(myTask.relateResource[0])
        if (this.memory.working)
        {
            this.transfer__(storage_,myTask.relateResource[0])
        }
        else
        {
            this.withdraw__(terminal_,myTask.relateResource[0])
        }
        this.LastMisson(2)

    }

    /* 处理仓送终端任务 */
    public handleSToT():void{
        var terminal_ = Game.getObjectById(this.room.memory.TaskMemory.terminalID as string) as StructureTerminal
        var storage_ = Game.getObjectById(this.room.memory.TaskMemory.storageID as string) as StructureStorage
        var myTask = this.room.getMisson('CREEP','仓送终端')
        if (!terminal_ || !storage_ || terminal_.store.getUsedCapacity(myTask.relateResource[0]) > Game.rooms[this.memory.belong].memory.TaskData.terminalResource[myTask.relateResource[0]].num)
        {
            Game.rooms[this.memory.belong].deleteMisson('CREEP','仓送终端')
            return
        }
        this.say('仓->终端')
        this.wipeResource(myTask.relateResource[0],storage_)
        this.workstate(myTask.relateResource[0])
        if (this.memory.working)
        {
            this.transfer__(terminal_,myTask.relateResource[0])
        }
        else
        {
            this.withdraw__(storage_,myTask.relateResource[0])
        }
        this.LastMisson(2)
    }

    /* 处理终端发送任务 */
    public handleTToT():void{
        var terminal_ = Game.getObjectById(this.room.memory.TaskMemory.terminalID as string) as StructureTerminal
        var storage_ = Game.getObjectById(this.room.memory.TaskMemory.storageID as string) as StructureStorage
        var myTask = this.room.getMisson('CREEP','资源传送')
        if (myTask.state == 0)
        {
            this.say("🚐1️⃣")
            this.wipeResource('energy',storage_)
            this.workstate('energy')
            if (this.memory.working) this.transfer__(terminal_,'energy')
            else{
                this.withdraw__(storage_,'energy')
                if (storage_.store['energy'] < this.store.getFreeCapacity('energy') && this.store['energy'] > 0)
                {
                    /* 说明资源不足 */
                    if (storage_.store['energy'] + this.store['energy'] + terminal_.store['energy'] >= myTask.expense)
                        this.memory.working = true
                    else
                        Game.rooms[this.memory.belong].deleteMisson_ID(myTask.ID)
                }
            }
        }
        else if (myTask.state == 1)
        {
            this.say("🚐2️⃣")
            this.wipeResource(myTask.relateResource[0],storage_)
            this.workstate(myTask.relateResource[0])
            var rType = myTask.relateResource[0]
            if (this.memory.working) this.transfer__(terminal_,rType)
            else{
                this.withdraw__(storage_,rType)
                if (storage_.store[rType] < this.store.getFreeCapacity(rType) && this.store[rType] > 0)
                {
                    if ( this.store[rType] + terminal_.store[rType] >= myTask.num)
                        this.memory.working = true
                    else
                        Game.rooms[this.memory.belong].deleteMisson_ID(myTask.ID)
                }
            }
            
        }
        else
        {
            this.say('🤑')
            this.wipeResource(myTask.relateResource[0],storage_)
        }
    }
}